﻿<!DOCTYPE html>
<html>
<head>
  <!-- 页面meta -->
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>传智健康</title>
  <meta name="description" content="传智健康">
  <meta name="keywords" content="传智健康">
  <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../plugins/elementui/index.css">
  <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="../css/style.css">
  <style>
    .datatable {
      position: relative;
      box-sizing: border-box;
      -webkit-box-flex: 1;
      width: 100%;
      max-width: 100%;
      font-size: 14px;
      color: rgb(96, 98, 102);
      overflow: hidden;
      flex: 1 1 0%;
    }
    .datatable td{
      padding: 12px;
      min-width: 0;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      text-overflow: ellipsis;
      vertical-align: middle;
      position: relative;
      text-align: center;
    }
    .t_header th{
      padding: 12px;
      min-width: 0;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      text-overflow: ellipsis;
      vertical-align: middle;
      position: relative;
      text-align: center;
    }
  </style>
</head>
<body class="hold-transition">
<div id="app">
  <div class="content-header">
    <h1>菜单设置<small>用户管理</small></h1>
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜单设置</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div class="app-container">
    <div class="box">
      <div class="filter-container">
        <el-input placeholder="电话/用户名" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
        <el-button @click="findPage()" class="dalfBut">查询</el-button>
        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
        <el-card class="box-card">
          <div class="boxMain">
            <el-button style="margin-bottom: 20px;margin-right: 20px" type="primary" @click="downloadTemplate()">模板下载</el-button>
            <el-upload action="/user/upload.do"
                       name="excelFile"
                       :show-file-list="false"
                       :on-success="handleSuccess"
                       :before-upload="beforeUpload">
              <el-button type="primary">导入数据</el-button>
            </el-upload>
          </div>
          <div>
            操作说明：请点击"模板下载"按钮获取模板文件，在模板文件中录入用户设置数据后点击"上传文件"按钮上传模板文件。
          </div>
        </el-card>
      </div>

      <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
        <el-table-column type="index" align="center" label="序号"></el-table-column>
        <el-table-column prop="username" label="用户名" align="center"></el-table-column>
        <el-table-column prop="gender" label="性别" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.gender == '1' ?'男' : '女'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="birthday" :formatter="formatDate" label="生日" align="center"></el-table-column>
        <el-table-column prop="telephone" label="电话" align="center"></el-table-column>

        <!--<el-table-column prop="station" label="是否启用" align="center"></el-table-column>-->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
                class="pagiantion"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-size="pagination.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="pagination.total">
        </el-pagination>
      </div>
      <!-- 新增标签弹层 -->
      <div class="add-form">
        <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
          <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="用户名" prop="username">
                  <el-input v-model="formData.username"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="性别" prop="gender">
                  <el-select v-model="formData.gender">
                    <el-option label="男" value="1"></el-option>
                    <el-option label="女" value="2"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="生日"  prop="birthday">
                  <el-input  v-model="formData.birthday" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="电话" prop="telephone">
                  <el-input v-model="formData.telephone"/>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="checkScrol">
              <table class="datatable">
                <thead>
                <tr>
                  <th>选择</th>
                  <th>序号</th>
                  <th>权限组名称</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="c in tableData">
                  <td>
                    <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                  </td>
                  <td><label :for="c.id">{{c.id}}</label></td>
                  <td><label :for="c.id">{{c.name}}</label></td>
                  <td><label :for="c.id">{{c.keyword}}</label></td>
                </tr>
                </tbody>
              </table>
            </div>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="handleAdd()">确定</el-button>
          </div>
        </el-dialog>
      </div>

      <!-- 编辑标签弹层 -->
      <div class="add-form">
        <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
          <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="用户名" prop="username">
                  <el-input v-model="formData.username"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="性别" prop="gender">
                  <el-select v-model="formData.gender">
                    <el-option label="男" value="1"></el-option>
                    <el-option label="女" value="2"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="生日" prop="birthday">
                  <el-input v-model="formData.birthday"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="电话" prop="telephone">
                  <el-input v-model="formData.telephone"/>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="checkScrol">
              <table class="datatable">
                <thead>
                <tr>
                  <th>选择</th>
                  <th>序号</th>
                  <th>权限组名称</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="c in tableData">
                  <td>
                    <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                  </td>
                  <td><label :for="c.id">{{c.id}}</label></td>
                  <td><label :for="c.id">{{c.name}}</label></td>
                  <td><label :for="c.id">{{c.remark}}</label></td>
                </tr>
                </tbody>
              </table>
            </div>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
            <el-button type="primary" @click="handleEdit()">确定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            tableData:[],//新增和编辑表单中对应的检查项列表数据
            roleIds:[],//新增和编辑表单中对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                username: [{ required: true, message: '用户名为必填项', trigger: 'blur' }]/*,
                password: [{ required: true, message: '密码为必填项', trigger: 'blur' }]*/
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            //调用分页查询方法完成分页查询
            this.findPage();
        },
        methods: {
            //分页查询
            findPage() {
                var param = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryString:this.pagination.queryString
                };

                //发送ajax请求，进行分页查询
                axios.post("/user/findPage.do",param).then((res) => {
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                }).catch((r) => {
                    this.showMessage(r);
                });

            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.roleIds=[];
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                //将添加窗口展示出来
                this.dialogFormVisible = true;
                axios.get("/role/findAllRole.do").then((res)=>{
                    if(res.data.flag){
                        this.tableData = res.data.data;
                    }else {
                        this.$message.error(res.data.message)
                    }
                })
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();
                this.dialogFormVisible4Edit = true;
                //回显数据
                axios.get("/user/findById.do?id=" + row.id).then((res) => {
                    if(res.data.flag){
                        this.formData = res.data.data;//为模型数据赋值，基于VUE数据绑定进行回显
                    }
                });
                axios.get("/role/findAllRole.do").then((res)=>{
                    if(res.data.flag){
                        this.tableData = res.data.data;
                        axios.get("/user/findRoleNameByUserId.do?userId="+row.id).then((res)=>{
                            if(res.data.flag){
                                this.roleIds = res.data.data;
                            }
                        })
                    }else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;//指定最新的页码
                this.findPage();
            },

            showMessage(r){
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },

            // 删除
            handleDelete(row) {
                //弹出确认框
                this.$confirm('你确定要删除当前数据吗？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送ajax请求，将id提交到Controller
                    axios.get("/user/delete.do?id=" + row.id).then((res) => {
                        if(res.data.flag){
                            this.$message({
                                type:'success',
                                message:res.data.message
                            });
                            this.findPage();
                        }else{
                            //执行失败
                            this.$message.error(res.data.message);
                        }
                    }).catch((r) => {
                        this.showMessage(r);
                    });
                }).catch(() => {
                    this.$message("已取消");
                });
            },
            beforeUpload(file){
                const isXLS = file.type === 'application/vnd.ms-excel';
                if(isXLS){
                    return true;
                }
                const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                if (isXLSX) {
                    return true;
                }
                this.$message.error('上传文件只能是xls或者xlsx格式!');
                return false;
            },
            //下载模板文件
            downloadTemplate(){
                window.location.href="../../template/user_template.xlsx";
            },
            //上传成功提示
            handleSuccess(response, file) {
                if(response.flag){
                    this.$message({
                        message: response.message,
                        type: 'success'
                    });
                }else{
                    this.$message.error(response.message);
                }
                console.log(response, file, fileList);
            },
            //编辑
            handleEdit() {
                axios.post("/user/edit.do?roleIds=" + this.roleIds, this.formData).then((res) => {
                    if (res.data.flag) {
                        this.dialogFormVisible4Edit = false;
                        this.$message({
                            type: 'success',
                            message: res.data.message
                        });
                        this.findPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).catch((r) => {
                    this.showMessage(r);
                });
            },
            //添加
            handleAdd () {
                axios.post("/user/add.do?roleIds="+this.roleIds,this.formData).then((res)=>{
                    if(res.data.flag){
                        this.dialogFormVisible = false;
                        this.$message({
                            type:'success',
                            message: res.data.message
                        });
                        this.findPage();
                            vue.$confirm('您的初始密码为1234，请登陆后自行进行修改!','提示',{
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                            })
                    }else{
                        this.$message.error(res.data.message);
                    }
                }).catch((r) => {
                    this.showMessage(r);
                });
            },


            formatDate(row, column) {
                // 获取单元格数据
                let data = row[column.property]
                if (!data) {
                    return ''
                }
                let dt = new Date(data)
                return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
            }

        }
    })
</script>
</html>